import React, { Component } from 'react'
import { Card, Form, Input, Button, Checkbox, message } from 'antd'
import styles from 'pages/Login/index.module.scss'
import logo from 'assets/logo.png'
import { loginAPI } from "api/user"
import { setToken } from 'utils/token'
export default class index extends Component {
  render() {
    return (
      <div className={styles.login}>
        <Card className='login-container'>
          <img className='login-logo' src={logo} alt="极客园" />
          <Form
            name="basic"
            autoComplete="off"
            onFinish={this.submitFn}
            initialValues={{
              agree: true,
              mobile: '13911111111',
              code: '246810',
            }}
          >
            <Form.Item
              name="mobile"
              rules={[{ required: true, message: '手机号码不能为空!' },
              { pattern: /^1[3-9][0-9]{9}$/, message: '手机号码格式错误' }
              ]}
            >
              <Input placeholder='请输入手机号码' />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[{ required: true, message: '验证码不能为空!' },
              { pattern: /^\d{6}$/, message: '验证码格式错误' }]}
            >
              <Input.Password placeholder='请输入验证码' />
            </Form.Item>

            <Form.Item name="agree" valuePropName="checked" rules={[
              {
                validator: (_, value) =>
                  value ? Promise.resolve() : Promise.reject(new Error('请阅读协议并同意'))
              }
            ]}>
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div >
    )
  }

  // 登录
  submitFn = async (values) => {
    // console.log(values);
    try {
      const res = await loginAPI(values)
      // console.log(res.data.token);
      // localStorage.setItem('geek_pc', res.data.token)
      setToken(res.data.token)
      // console.log(this.props.history.push);
      this.props.history.push('/Layout')
      message.success('登录成功', 1.5)
    } catch (error) {
      // console.dir(error.response.data.message);
      message.error(error.response.data.message, 1.5)
    }
  }

}
